package com.bestvike.stone.spring.trace.scheduling;

import com.bestvike.stone.core.lang.ThrowableUtils;
import com.bestvike.stone.spring.mail.bugreport.BugReportLevel;
import com.bestvike.stone.spring.mail.bugreport.BugReportUtils;
import lombok.extern.apachecommons.CommonsLog;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by 许崇雷 on 2018-01-20.
 */
@CommonsLog
public final class IncomingLog {
    public static void writeBeginLog(String action) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("action", action);
        log.info("==>Scheduled Begin: " + map);
    }

    public static void writeEndLog(String action, long tookMs) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("action", action);
        map.put("took", tookMs);
        log.info("==>Scheduled End: " + map);
    }

    public static void writeErrorLog(String action, Exception e, long tookMs) {
        String msg = ThrowableUtils.getString(e);
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("action", action);
        map.put("error", msg);
        map.put("took", tookMs);
        log.error("==>Scheduled Error: " + map);
        //错误报告
        BugReportUtils.sendAsync(BugReportLevel.ERROR, msg);
    }
}
